const specialChars = [
  { text: 'exclamation mark', value: '123' },
  { text: 'at', value: '456' },
  { text: 'hash', value: '789' }
]

export const useInit = (props: Record<string, any>) => {
  return {
    height: props.height,
    language_url: '/tinymce/langs/zh-Hans.js',
    language: 'zh-Hans',
    skin_url: '/tinymce/skins/ui/oxide', // 皮肤：这里引入的是public下的资源文件
    // skin_url: 'tinymce/skins/ui/oxide-dark',//黑色系
    content_css: '/tinymce/skins/content/default/content.css', //内容区域css样式
    content_style: 'p {margin: 5px 0;}',
    plugins:
      'save accordion lists advlist quickbars autolink link autosave charmap wordcount directionality emoticons fullscreen image importcss insertdatetime nonbreaking pagebreak preview searchreplace table visualblocks formatpainter kityformula-editor',
    toolbar: `save undo redo restoredraft code pastetext formatpainter removeformat | forecolor backcolor styles blocks fontfamily fontsize bold italic underline strikethrough lineheight | alignleft alignright aligncenter alignjustify
      outdent indent ltr rtl | bullist numlist | blockquote subscript superscript  | link table image charmap emoticons hr pagebreak insertdatetime
      | print preview fullscreen | wordcount nonbreaking searchreplace visualblocks custom-btn`,
    toolbar_mode: 'sliding', // 工具栏模式: floating sliding scrolling wrap
    external_plugins: {
      // 引入自定义的插件
      'kityformula-editor': '/tinymce/plugins/kityformula-editor/plugin.min.js',
      formatpainter: '/tinymce/plugins/formatpainter/plugin.min.js'
    },
    menubar: false, // 菜单栏
    statusbar: true, // 是否显示底部状态栏
    auto_focus: true, // 自动聚焦
    branding: false, // 默认会在右下角显示Tiny图标
    promotion: false, // 隐藏upgrade按钮
    resize: true, // 改变宽高
    // 快速工具栏配置，需引入插件 quickbars
    quickbars_selection_toolbar: 'bold italic underline strikethrough', // 设置 快速选择 触发提供的工具栏 需引入插件  默认 'alignleft aligncenter alignright' 设置为false禁用
    quickbars_insert_toolbar: 'quickimage quicktable quicklink', // 设置 快速插入 触发提供的工具栏 需引入插件quickbars 默认 quickimage quicktable 设置为false禁用
    quickbars_image_toolbar: 'alignleft aligncenter alignright',
    save_onsavecallback: function () {},
    automatic_uploads: false, // 禁止图片自动上传
    font_size_formats: '12px 14px 16px 18px 24px 36px 48px 56px 72px',
    font_family_formats:
      '微软雅黑=Microsoft YaHei,Helvetica Neue,PingFang SC,sans-serif;苹果苹方=PingFang SC,Microsoft YaHei,sans-serif;宋体=simsun,serif;仿宋体=FangSong,serif;黑体=SimHei,sans-serif;Arial=arial,helvetica,sans-serif;Arial Black=arial black,avant garde;Book Antiqua=book antiqua,palatino;Comic Sans MS=comic sans ms,sans-serif;Courier New=courier new,courier;Georgia=georgia,palatino;Helvetica=helvetica;Impact=impact,chicago;Tahoma=tahoma,arial,helvetica,sans-serif;Terminal=terminal,monaco;Times New Roman=times new roman,times;Verdana=verdana,geneva;Webdings=webdings;Wingdings=wingdings,zapf dingbats;',
    setup: (editor: any) => {
      // editor.ui.registry.addButton('custom-btn', {
      //   text: '自定义按钮',
      //   icon: 'ai',
      //   tooltip: '自定义按钮',
      //   onAction: function () {}
      // })
      const onAction = (autocompleteApi: any, rng: any, value: any) => {
        editor.selection.setRng(rng)
        // editor.insertContent(value);
        // contenteditable=false 不可编辑，保证删除时#和内容为一个整体
        editor.insertContent(
          `<span style="color: blue;" contenteditable="false">#${value}</span><span>&nbsp;</span>`
        )
        autocompleteApi.hide()
      }

      const getMatchedChars = (pattern: any) => {
        return specialChars.filter((char) => char.text.indexOf(pattern) !== -1)
      }
      /* 自动补全 */
      editor.ui.registry.addAutocompleter('specialchars_cardmenuitems', {
        trigger: '#',
        minChars: 0,
        columns: 1,
        highlightOn: ['char_name'],
        onAction: onAction,
        fetch: (pattern: any) => {
          return new Promise((resolve) => {
            const results = getMatchedChars(pattern).map((char) => ({
              type: 'cardmenuitem',
              value: char.value,
              label: char.text,
              items: [
                {
                  type: 'cardcontainer',
                  direction: 'vertical',
                  items: [
                    {
                      type: 'cardtext',
                      text: char.text,
                      name: 'char_name'
                    },
                    {
                      type: 'cardtext',
                      text: char.value
                    }
                  ]
                }
              ]
            }))
            resolve(results)
          })
        }
      })
    },
    // 初始化完成
    init_instance_callback: (editor: any) => {
      editor.on('blur', (_e: any) => {
        // var content = tinyMCE.activeEditor.getContent();
      })
    },
    // 此处为自定义图片上传处理函数
    images_upload_handler: (blobInfo: any) =>
      new Promise((resolve: any, reject: any) => {
        let file = blobInfo.blob()
        if (file.size / 1024 / 1024 > 200) {
          reject({
            message: '上传失败，图片大小请控制在 200M 以内',
            remove: true
          })
        }
        // 调接口
        console.log(file, resolve, reject, '==file')
        // let url = 'blob:http://127.0.0.1:8000/2c6c7bbe-cb9a-481d-acfe-adc00f2866d5'
        // resolve(url)
      }),
    image_advtab: true, // 图片高级功能
    lists_indent_on_tab: true, // 是否按tab键进入新的子列表
    link_context_toolbar: true,
    link_rel_list: [
      { title: 'No Referrer', value: 'noreferrer' },
      { title: 'External Link', value: 'external' }
    ],
    link_target_list: [
      { title: 'None', value: '' },
      { title: 'Same page', value: '_self' },
      { title: 'New page', value: '_blank' },
      { title: 'Parent frame', value: '_parent' }
    ],
    fullscreen_native: true,
    pagebreak_split_block: true,
    table_border_widths: [
      { title: 'small', value: '1px' },
      { title: 'medium', value: '3px' },
      { title: 'large', value: '5px' }
    ],
    table_toolbar:
      'tableprops tabledelete | tableinsertrowbefore tableinsertrowafter tabledeleterow | tableinsertcolbefore tableinsertcolafter tabledeletecol tablecellborderwidth tablecellborderstyle'
  }
}
